package com.largeOrange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.largeOrange.common.R;
import com.largeOrange.dto.DishDto;
import com.largeOrange.pojo.Category;
import com.largeOrange.pojo.Dish;
import com.largeOrange.service.CategoryService;
import com.largeOrange.service.DishFlavorService;
import com.largeOrange.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 大橙子
 * @version 1.0
 * @since 1.0
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    CategoryService categoryService;

    /**
     * 菜品管理分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> dishPage(int page,int pageSize,String name){
        //分页查询对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        //查询条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        dishLambdaQueryWrapper.like(name!=null,Dish::getName,name);
        dishLambdaQueryWrapper.orderByAsc(Dish::getId);
        //分页查询结果集
        dishService.page(dishPage, dishLambdaQueryWrapper);
        //dto分页对象
        Page<DishDto> dtoPage = new Page<>();
        //分页信息拷贝
        BeanUtils.copyProperties(dishPage,dtoPage,"records");
        //所有的dish对象
        List<Dish> dishList = dishPage.getRecords();
        //将分页对象的List<Dish>属性转换为List<DishDto>
        List<DishDto> dishDos = new ArrayList<>();
        //遍历集合设置DishDto的名字属性
        dishList.forEach(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(dish.getCategoryId()!=null,Category::getId,dish.getCategoryId());
            Category category = categoryService.getOne(wrapper);
            dishDto.setCategoryName(category.getName());
            dishDos.add(dishDto);
        });
        //开始转换
        dtoPage.setRecords(dishDos);
        return R.success(dtoPage);
    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        log.info(dishDto.getDescription());
        dishService.saveDishAndFlavor(dishDto);
        return R.success("保存成功！");
    }

    /**
     * 修改菜品之数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Dish> appear(@PathVariable Long id){
        DishDto dishDto = dishService.dishFlavorAppear(id);
        return R.success(dishDto);
    }

    /**
     * 保存数据
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavor(dishDto);
        return R.success("修改成功！");
    }

    /**
     * 菜品状态批量停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> status(Long[] ids){
        for (Long id : ids) {
            Dish dish = dishService.getById(id);
            dish.setStatus(0);
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId,id);
            dishService.update(dish,wrapper);
        }
        return R.success("已停售");
    }
    /**
     * 菜品状态批量起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> statusMany(Long[] ids){
        for (Long id : ids) {
            Dish dish = dishService.getById(id);
            dish.setStatus(1);
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId,id);
            dishService.update(dish,wrapper);
        }
        return R.success("已起售");
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        for (Long id : ids) {
            log.info("==================>"+id);
        }
        dishService.deleteDishAndFlavor(ids);
        return R.success("删除成功！");
    }
    @GetMapping("/list")
    public R<List<Dish>> SetMealList(Long categoryId){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        //展示起售菜品
        wrapper.eq(Dish::getStatus,1);
        List<Dish> dishes = dishService.list(wrapper);
        return R.success(dishes);
    }
}
